package math.vectorSpace.R1;

import math.vectorSpace.R3.Point3D;
import math.vectorSpace.RX.Vector;

/**
 * Class for creating and working with vectors in R1.
 * Contains extensive methods for manipulating vectors.
 * 
 * @author Shimu Wu
 * @version 1.0
 */
public class Vector1D extends Vector<Vector1D> {

    /**
     * Constructs and initialises the zero vector.
     */
    public Vector1D() {
        this(0);
    }

    /**
     * Constructs and initialises a vector with the specified x component.
     *
     * @param x the x-component of this vector
     */
    public Vector1D(double x) {
        super(new double[]{x});
    }

    /**
     * Constructs and initialises a vector that extends from point1 to point2.
     *
     * @param point1 a {@link Point3D} at the tail of the vector
     * @param point2 a {@link Point3D} at the head of the vector
     */
    public Vector1D(Point1D point1, Point1D point2) {
        this(point2.getX() - point1.getX());
    }

    /**
     * Constructs and initialises a vector that starts from the origin
     * and extends to the given point.
     *
     * @param point a {@link Point3D} object
     */
    public Vector1D(Point1D point) {
        this(point.getX());
    }

    /**
     * Returns the x component of this vector.
     *
     * @return the x-component
     */
    public double getX() {
        return super.getComponent(0);
    }

    /**
     * Sets the x component of this vector.
     *
     * @param x the x-component
     */
    public void setX(double x) {
        super.setComponent(0, x);
    }

    /**
     * Sets the x component of this vector to the x component of the given vector.
     *
     * @param vector a Vector1D object
     */
    public void setX(Vector1D vector) {
        setX(vector.getX());
    }

    /**
     * Sets the x component of this vector to the x coordinate of the given point.
     *
     * @param point a Point1D object
     */
    public void setX(Point1D point) {
        setX(point.getX());
    }

    /**
     * Returns the String representation of this vector of the form: Vector1D [x]
     *
     * @return the String representation of this vector
     */
    @Override
    public String toString() {
        return "Vector1D [" + Double.toString(this.getX()) + "]";
    }

    @Override
    protected Vector1D createVector(double[] components) {
        return new Vector1D(components[0]);
    }
}
